package com.Txunda.fxw.ui.activity.other;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.Txunda.fxw.R;
import com.Txunda.fxw.base.utils.util.JSONUtils;
import com.Txunda.fxw.base.utils.util.MapUtils;
import com.Txunda.fxw.ui.base.BaseAty;
import com.Txunda.fxw.ui.fgt.Business2Fgt;
import com.Txunda.fxw.ui.fgt.WebAty;
import com.Txunda.fxw.ui.http.Company;
import com.Txunda.fxw.ui.http.Syb;
import com.bumptech.glide.Glide;
import com.itheima.pulltorefreshlib.PullToRefreshBase;
import com.itheima.pulltorefreshlib.PullToRefreshListView;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 收藏模块
 *
 * @author Administrator
 */
public class CollectAty extends BaseAty {

    @ViewInject(R.id.collect_lv)
    private PullToRefreshListView collect_lv;
    @ViewInject(R.id.aty_title2_tv_center)
    private TextView aty_title2_tv_center;
    @ViewInject(R.id.aty_title_img_right_message_2)
    private ImageView aty_title_img_right_message_2;
    private List<Map<String, String>> list_data;
    private MyAdapter adapter;
    private Bundle bundle;
    private int p = 1;

    private SharedPreferences sp;
    private SharedPreferences.Editor edit;
    private Company company;
    private StringBuffer sb = new StringBuffer("[");
    private List<Map<String, String>> collect_more_data;
    private ImageLoader imageLoader;

    @ViewInject(R.id.icon_no_data)
    private TextView icon_no_data;
    private Syb syb;
    private String com_ida;
    private    String class_ida;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        syb = new Syb();
        //取值
        String com_id = sp.getString("history_collect", "");    //商家id
        sb.append(com_id).append("]");

        showProgressDialog();
        company.collectList(sb.toString(), p, this);
        collect_lv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {

            @Override
            public void onPullDownToRefresh(PullToRefreshBase refreshView) {
                p = 1;
                showProgressDialog();
                company.collectList(sb.toString(), p, CollectAty.this);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase refreshView) {
                p++;
                showProgressDialog();
                company.collectList(sb.toString(), p, CollectAty.this);
            }
        });
        collect_lv.onRefreshComplete();
        collect_lv.setEmptyView(icon_no_data);

        collect_lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                com_ida = list_data.get(position - 1).get("com_id");
                class_ida = list_data.get(position - 1).get("class_id");
                String name = list_data.get(position - 1).get("name");

                syb.sybInfo(class_ida, CollectAty.this);
                finish();
            }
        });


    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        showProgressDialog();
        company.collectList(sb.toString(), p, this);
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.aty_collect_aty;
    }

    @SuppressLint("WrongConstant")
    @Override
    protected void initialized() {
        collect_more_data = new ArrayList<Map<String, String>>();
        company = new Company();
        sp = getSharedPreferences("COLLECT", Activity.MODE_APPEND);
        edit = sp.edit();
        aty_title2_tv_center.setText("收藏");
        aty_title_img_right_message_2.setVisibility(View.GONE);
        list_data = new ArrayList<Map<String, String>>();
        adapter = new MyAdapter();
    }

    @Override
    public void onComplete(String requestUrl, String jsonStr) {
        super.onComplete(requestUrl, jsonStr);

        if (requestUrl.contains("Syb/sybInfo")) { // 商家详情
            Map<String, String> map = JSONUtils.parseKeyAndValueToMap(jsonStr);
            Map<String, String> data = JSONUtils.parseKeyAndValueToMap(map.get("data"));
            if (MapUtils.isEmpty(data)) {
                return;
            }
            //电话
            //图文链接
            Bundle bundle = new Bundle();
            bundle.putString("type", "fb");
            bundle.putString("class_id", class_ida);
            bundle.putString("com_id", com_ida);
            bundle.putString("tel", data.get("tel"));
            bundle.putString("url", data.get("url"));
            startActivity(WebAty.class, bundle);
        }

        if (requestUrl.contains("collectList")) {
            Map<String, String> map = JSONUtils.parseKeyAndValueToMap(jsonStr);
            if ("success".equals(map.get("flag"))) {
                if (p == 1) {
                    list_data = JSONUtils.parseKeyAndValueToMapList(map.get("data"));
                    collect_lv.setAdapter(adapter);
                } else {
                    collect_more_data = JSONUtils.parseKeyAndValueToMapList(map.get("data"));
                    list_data.addAll(collect_more_data);
                    adapter.notifyDataSetChanged();
                }
                collect_lv.onRefreshComplete();
                collect_lv.setEmptyView(icon_no_data);
            }

        }
    }

    @Override
    protected void requestData() {
    }

    /**
     * 收藏adapter
     *
     * @author Administrator
     */
    public class MyAdapter extends BaseAdapter {
        MyViewHolder vh;
        @Override
        public int getCount() {
            int ret = 0;
            if (list_data != null) {
                ret = list_data.size();
            }
            return ret;
        }

        @Override
        public Object getItem(int position) {
            return list_data.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @SuppressLint("SetTextI18n")
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = LayoutInflater.from(CollectAty.this).inflate(R.layout.item_collecy_aty, null);
                vh = new MyViewHolder();
                ViewUtils.inject(vh, convertView);
                convertView.setTag(vh);
            } else {
                vh = (MyViewHolder) convertView.getTag();
            }
            Glide.with(CollectAty.this).load(list_data.get(position).get("cover_pic")).into(vh.item_collect_img);
            vh.item_collect_title.setVisibility(View.VISIBLE);
            vh.item_collect_title.setText(list_data.get(position).get("name"));   //标题
            vh.item_collect_money.setText("人均消费: " + list_data.get(position).get("consume"));
            vh.item_collect_time.setText("营业时间: " + list_data.get(position).get("shop_hours"));   //营业时间
            vh.item_collect_address.setText("营业地址: " + list_data.get(position).get("address"));   //营业地址
            return convertView;
        }

        class MyViewHolder {
            @ViewInject(R.id.item_collect_img)
            private ImageView item_collect_img;
            @ViewInject(R.id.item_collect_title)
            private TextView item_collect_title;
            @ViewInject(R.id.item_collect_money)
            private TextView item_collect_money;
            @ViewInject(R.id.item_collect_time)
            private TextView item_collect_time;
            @ViewInject(R.id.item_collect_address)
            private TextView item_collect_address;
        }
    }
}
